﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eCitizen.eAlerts.Constants;
using eCitizen.DataContracts;
using eCitizen.DataAccess;

namespace eCitizen.eAlerts.BusinessService
{
    public class SubscriberManager : ServiceBase
    {
        #region "Get Subscribers for Message Broadcast : SendAlerts"
        
        public List<Subscriber> GetSubscribersforBroadcast(List<RequestEntry> eAlertRequests)
        {
            List<Subscriber> subscriberList = new List<Subscriber>();
            SubscriberInfoService subInfoService = new SubscriberInfoService();

            foreach (RequestEntry request in eAlertRequests)
            {
                List<Subscriber> currentRequestSubscribers = new List<Subscriber>();
                // 1.1 Check the request if it's a broadcast request, or an individual message
                #region "Get Subscribers based on the Request"
                if (request.TargetNricOrAll.Equals(ServiceConstants.BroadcastMessageIndicator, StringComparison.CurrentCultureIgnoreCase))
                {
                    #region"Broadcast Message"
                    // This is a broadcast message, get all the subscribers that matches the criteria

                    // Broadcast can be two different types 1). Filter by SubscriberProfile 2). Broadcast to all subscribers under the Subscription
                    SubscriberProfile searchCrteria = new SubscriberProfile();
                    if (!TryCreateSearchCriteria(request, out searchCrteria))
                    {
                        // Search Subscribers which matches the SubscriberProfile
                        currentRequestSubscribers = subInfoService.GetSubscriberByProfile(searchCrteria);
                    }
                    else
                    {
                        currentRequestSubscribers = GetSubscriberListByService(request.ServiceId);

                    }

                    #endregion
                }
                else
                {
                    #region "Individual Message"
                    // This is an individual message, get the subscriber with the NRIC No
                    Subscriber subscriberInfo = new Subscriber();

                    // Get Subscriber data based on the NRIC
                    subscriberInfo = subInfoService.GetSuscriberByNRIC(request.TargetNricOrAll);
                    currentRequestSubscribers.Add(subscriberInfo);

                    #endregion
                }
                #endregion

                // 1.2 Set the Broadcast-related information into the Subscriber
                currentRequestSubscribers = SetBroadcastRequestInfo(request, currentRequestSubscribers);

                // 1.3 Get the subscriptions from eCitizen System using the list of the Subscribers
                currentRequestSubscribers = subInfoService.GetSubscriptionbySubscriberList(currentRequestSubscribers);

                // 1.4 Assign the RequestId which is meant for Logging Purposes
                currentRequestSubscribers = AssignRequestInfoToSubscribers(request.RequestId, currentRequestSubscribers);

                subscriberList.AddRange(currentRequestSubscribers);

            }
            
            return subscriberList;
        }

        private bool TryCreateSearchCriteria(RequestEntry request,out SubscriberProfile searchProfile)
        {
            bool isEmptyProfile = true;

            SubscriberProfile searchCrteria = new SubscriberProfile();

            searchCrteria.Gender = request.Gender;
            searchCrteria.AgeRangeStartDate = request.BirthStartDate;
            searchCrteria.AgeRangeEndDate = request.BirthEndDate;
            searchCrteria.EmploymentStatus = request.EmploymentStatus;
            searchCrteria.MaritalStatus = request.MartialStatus;
            searchCrteria.ResidentialStatus = request.ResidentialStatus;

            searchProfile = searchCrteria;

            isEmptyProfile = (string.IsNullOrEmpty(searchCrteria.Gender) && !searchCrteria.AgeRangeStartDate.HasValue && !searchCrteria.AgeRangeEndDate.HasValue && String.IsNullOrEmpty(searchCrteria.EmploymentStatus) && string.IsNullOrEmpty(searchCrteria.MaritalStatus) && string.IsNullOrEmpty(searchCrteria.ResidentialStatus));

            return isEmptyProfile;
        }

        private List<Subscriber> AssignRequestInfoToSubscribers(int requestId,List<Subscriber> subscriberList)
        {
            List<Subscriber> modifiedList = new List<Subscriber>();

            foreach (Subscriber subscriberInfo in subscriberList)
            {
                Subscriber modifiedInfo = subscriberInfo;
                modifiedInfo.RequestId = requestId;

                modifiedList.Add(modifiedInfo);
            }
            return modifiedList;
        }
        private List<Subscriber> SetBroadcastRequestInfo(RequestEntry request, List<Subscriber> subscriberList)
        {
            foreach (Subscriber subscriberInfo in subscriberList)
            {
                subscriberInfo.SubscriptionAgencyCode = request.ServiceId;
                subscriberInfo.DateDue = request.DateDue;
                subscriberInfo.DateExpired = request.DateExpired;
                subscriberInfo.MessageDescription = request.MessageDescription;
                subscriberInfo.AmountDue = request.AmountDue;
                subscriberInfo.PaymentStatus = request.PaymentStatus;
            }

            return subscriberList;
        }
        #endregion

        #region "Get Subscribers for SubscriberList Report : SendReports"

        public List<Subscriber> GetSubscriberListByService(string agencyServiceCode)
        {
            SubscriberInfoService subInfoService = new SubscriberInfoService();
            List<Subscriber> subscriberList = new List<Subscriber>();

            subscriberList = subInfoService.GetSubscriptionbyServiceCode(agencyServiceCode);

            subscriberList = subInfoService.GetSubscriberbySubscriptionList(subscriberList);

            return subscriberList;

        }
        #endregion
    }
}
